#! c:\\Perl\\bin\\perl.exe w

use strict;
use ADN::LIBRARY::SOAP::ClientSOAP;
use ADN::XML::OBJtoXML;
use ADN::CONF::Read;
use MIME::Base64;

my $ip = $ARGV[0];
my $Srep = $ARGV[1];
my $param = $ARGV[2];

main();
exit 0;

sub main {

  my ( $xml, $nbr, $conf, $file, $result, $line );
  my ( @tab, @prm, @result );

  @tab = split /\%\!\%\*\%\~\%/smx, $param;
  $nbr = scalar @tab;
  print "NBR : $nbr\n";

  for ( my $i = 0; $i < $nbr; $i++ ) {
    @prm = split /\@\#\@\*\@\~\@/smx, $tab[$i];
  }

  $nbr = scalar @prm;
  print "NBR PRM : $nbr\n";

  if (($nbr != 2) && ($nbr != 1)) {
    print 'Erreur ARG !!!';
    exit 0;
  }

  print "'Mode : $prm[0]\n";
  print "\nData encode : $prm[1]\n" if (defined($prm[1]));
  $prm[1] = decode_base64($prm[1]) if (defined($prm[1]));
	print "\nData decode : $prm[1]\n" if (defined($prm[1]));

	$prm[0] =~ tr/[A-Z]/[a-z]/;

  $xml = ADN::XML::OBJtoXML->new();
  $conf = ADN::CONF::Read->new();
  $file = $conf->_send_path().'conf.xml';

	if (($prm[0] =~ m/^[a-zA-Z]+$/smx)) {
		if ($prm[0] eq 'read') {
			if (-f $file && -r $file) {
				if (open FILE, '<', $file) {
					while( defined( $line = <FILE> ) ) {
						$result .= $line;
					}
					if (!close FILE) {}
				}
			}
			$result = encode_base64($result);
		}
		elsif (($prm[0] eq "write") && defined $prm[1]) {
			if (-f $file && -r $file) {
				if (open FILE, '>', $file) {
					$prm[1] =~ s/^\s*\<\?\w+\s+\w+\=\"[0-9\.]+\"\?\>\s*(\S.*)$/$1/;
					$prm[1] =~ s/(\>)\s*(\<[\/]?\w)/$1\n$2/g;
					print FILE $prm[1];
					if (!close FILE) {}
					$result = 'OK';
				}
				else {
    			$result = 'ERREUR';				
				}
			}
			else {
    		$result = 'ERREUR';			
			}
		}
		else {
    	$result = 'ERREUR';
		}
	}
	else {
		$result = 'ERREUR';
	}
	$xml->Data($result);
  @result = $xml->Configuration($xml);

	print 'DATA SEND : ';
  print @result;

  ADN::LIBRARY::SOAP::ClientSOAP->new( Proxy => $ip,
						                           Service => $Srep,
						                           Data => \@result );
  return;
}